package com.contactmgr.utils;

import java.util.ArrayList;
import java.util.List;

import android.content.ContentResolver;
import android.database.Cursor;
import android.provider.ContactsContract;
import android.util.Log;

import com.contactmgr.logObjects.Contact;

public class ContactReader {

	public static List<Contact> getContacts(ContentResolver cr) {
		Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
		List<Contact> contacts = new ArrayList<Contact>();
		if (cur.getCount() > 0) {
			while (cur.moveToNext()) {
				Contact tmpContact = new Contact();
				// read id
				String id = cur.getString(cur.getColumnIndex(ContactsContract.Contacts._ID));
				tmpContact.setContactId(id);
//				System.out.println("###########################");
//				System.out.println("For id : " + id);
//				System.out.println("###########################");
//				// ************* Get Values for this id ********/

				// read names
				String displayName = cur.getString(cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
				tmpContact.setDisplayName(displayName);

				// Cursor nameCur = cr.query(ContactsContract.Data.CONTENT_URI,
				// null,
				// ContactsContract.CommonDataKinds.StructuredName.CONTACT_ID
				// + " = ?", new String[] { id }, null);
				// while (nameCur.moveToNext()) {
				// String dname =
				// nameCur.getString(nameCur.getColumnIndex(ContactsContract.CommonDataKinds.StructuredName.DISPLAY_NAME));
				// String gname =
				// nameCur.getString(nameCur.getColumnIndex(ContactsContract.CommonDataKinds.StructuredName.GIVEN_NAME));
				// String fname =
				// nameCur.getString(nameCur.getColumnIndex(ContactsContract.CommonDataKinds.StructuredName.FAMILY_NAME));
				//
				// Log.i("Disp name", getNotNull(dname));
				// Log.i("Given name", getNotNull(gname));
				// Log.i("First Name", getNotNull(fname));
				// tmpContact.setDisplayName(dname);
				// tmpContact.setGivenName(gname);
				// tmpContact.setFamilyName(fname);
				// }
				// nameCur.close();

				/*** Phone Numbers ***/
				// Query phone
//				String hasPhone = cur.getString(cur.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER));
//				if (Boolean.parseBoolean(hasPhone)) {
					Cursor pCur = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,
							ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ?", new String[] { id }, null);
					while (pCur.moveToNext()) {
						String number = pCur.getString(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
						String typeStr = pCur.getString(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.TYPE));
						Contact.TYPE type = getPhoneType(typeStr);
						tmpContact.addPhone(type, number);

//					}
					
				}
					pCur.close();
				/*** EMAIL **/
				Cursor emailCur = cr.query(ContactsContract.CommonDataKinds.Email.CONTENT_URI, null,
						ContactsContract.CommonDataKinds.Email.CONTACT_ID + " = ?", new String[] { id }, null);
				while (emailCur.moveToNext()) {
					// This would allow you get several email addresses
					// if the email addresses were stored in an array
					String email = emailCur.getString(emailCur.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA));
					String emailType = emailCur.getString(emailCur.getColumnIndex(ContactsContract.CommonDataKinds.Email.TYPE));

					Contact.TYPE type = getEmailType(emailType);
					tmpContact.addEmail(type, email);

				}
				emailCur.close();

				// add
				contacts.add(tmpContact);
			}
		}
		return contacts;
	}

	public static void read(ContentResolver cr) {
		Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
		while (cur.moveToNext()) {
			String id = cur.getString(cur.getColumnIndex(ContactsContract.Contacts._ID));
			// read names
			Cursor nameCur = cr.query(ContactsContract.Data.CONTENT_URI, null, ContactsContract.CommonDataKinds.StructuredName.CONTACT_ID + " = ?",
					new String[] { id }, null);
			Log.i("Contact_ID :" + id, "-------------------");
			// the following reads display name
			System.out.println(cur.getString(cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME)));

			while (nameCur.moveToNext()) {
				String dname = nameCur.getString(nameCur.getColumnIndex(ContactsContract.CommonDataKinds.StructuredName.DISPLAY_NAME));
				String gname = nameCur.getString(nameCur.getColumnIndex(ContactsContract.CommonDataKinds.StructuredName.GIVEN_NAME));
				String fname = nameCur.getString(nameCur.getColumnIndex(ContactsContract.CommonDataKinds.StructuredName.FAMILY_NAME));

				Log.i("Names      :", "-------------------");
				Log.i("Disp name  :", getNotNull(dname));
				Log.i("Given name :", getNotNull(gname));
				Log.i("First Name :", getNotNull(fname));
			}
			nameCur.close();
		}
	}

	private static String getNotNull(String str) {
		if (str == null) {
			return "";
		}
		return str;
	}

	/**
	 * The following index were taken from :
	 * 
	 * http://developer
	 * .android.com/reference/android/provider/ContactsContract.CommonDataKinds
	 * .Email.html
	 * 
	 * http://developer.android.com/reference/android/provider/
	 * ContactsContract.CommonDataKinds.Phone.html
	 **/
	private static Contact.TYPE getPhoneType(String index) {
		if (index.trim().equals("1")) {
			// return "home";
			return Contact.TYPE.HOME;
		} else if (index.trim().equals("2")) {
			// return "mobile";
			return Contact.TYPE.MOBILE;
		} else if (index.trim().equals("3")) {
			// return "work";
			return Contact.TYPE.WORK;
		} else if (index.trim().equals("7")) {
			return Contact.TYPE.OTHER;
			// return "other";
		} else {
			// return "?";
			return Contact.TYPE.UNKNOWN;
		}
	}

	private static Contact.TYPE getEmailType(String index) {
		if (index.trim().equals("1")) {
			// return "home";
			return Contact.TYPE.HOME;
		} else if (index.trim().equals("2")) {
			// return "work";
			return Contact.TYPE.WORK;
		} else if (index.trim().equals("3")) {
			// return "other";
			return Contact.TYPE.OTHER;
		} else if (index.trim().equals("4")) {
			// return "mobile";
			return Contact.TYPE.MOBILE;
		} else {
			// return "?";
			return Contact.TYPE.UNKNOWN;
		}
	}
}
